   #include "shapes.h"

    shapes::shapes(QObject *parent) :
        QObject(parent), QGraphicsItem(){}

    shapes::~shapes(){}

    void circle::paint(QPainter *painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
    {
        QConicalGradient grad(QPointF(-size/2, -size/2), 90);
        grad.setColorAt(0, Qt::white);
        grad.setColorAt(0.5, Qt::yellow);
        grad.setColorAt(1, Qt::red);
        painter->setPen(pen);
        painter->setBrush(grad);
        painter->drawEllipse(-size/2, -size/2, size, size);
        ang = this->rotation();
        this->update(boundingRect());
        this->setFlag(ItemIsSelectable, true);
        this->setFlag(ItemIsFocusable,true);
        this->setFlag(ItemIsMovable,true);
        Q_UNUSED(option);
        Q_UNUSED(widget);
    }
    QRectF circle::boundingRect() const
    {
        return QRectF (-size/2, -size/2, size, size);
    }
    QPainterPath circle::shape() const
    {
        QPainterPath path;
        path.addEllipse(-size/2, -size/2, size, size);
        return path;
    }

    void triangle::paint(QPainter *painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
    {
        QPolygon polygon;
        polygon << QPoint(size/3, -size/2) << QPoint(size/3, size/2) << QPoint(-size/3, size/2);
        QConicalGradient grad(QPointF(-size/3, -size/3), 60);
        grad.setColorAt(0, Qt::white);
        grad.setColorAt(0.5, Qt::magenta);
        grad.setColorAt(1, Qt::darkMagenta);
        painter->setPen(pen);
        painter->setBrush(grad);
        painter->drawPolygon(polygon);
        ang = this->rotation();

        this->update(boundingRect());
        this->setFlag(ItemIsSelectable, true);
        this->setFlag(ItemIsFocusable,true);
        this->setFlag(ItemIsMovable,true);
        Q_UNUSED(option);
        Q_UNUSED(widget);
    }
    QRectF triangle::boundingRect() const
    {
        return QRectF (-size/3, -size/2, size, size);
    }
    QPainterPath triangle::shape() const
    {
        QPainterPath path;
        QPainterPath tr;
        QPoint p0(size/3, -size/2), p1(size/3, size/2), p2(-size/3, size/2);
        tr.moveTo(p0);
        tr.lineTo(p1);
        tr.lineTo(p2);
        tr.lineTo(p0);
        path.addPath(tr);
        return path;
    }

    void square::paint(QPainter *painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
    {
        QLinearGradient grad(-size/2+10, -size/2+10, size, size);
        grad.setColorAt(0, Qt::white);
        grad.setColorAt(0.5, Qt::green);
        grad.setColorAt(1, Qt::darkGreen);
        painter->setPen(pen);
        painter->setBrush(grad);
        painter->drawRect(-size/2,-size/2,size,size);
        ang = this->rotation();
        //this->update(boundingRect());
        this->update(boundingRect());
        this->setFlag(ItemIsSelectable, true);
        this->setFlag(ItemIsFocusable,true);
        this->setFlag(ItemIsMovable,true);
        Q_UNUSED(option);
        Q_UNUSED(widget);
    }
    QRectF square::boundingRect() const
    {
        return QRectF (-size/2, -size/2, size, size);
    }

    void shapes::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
        this->setPos(mapToScene(event->pos()));
        QPointF point = this->pos();
        this->coordX = point.x();
        this->coordY = point.y();
    }

    void shapes::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {

        if(event->button() == Qt::LeftButton)
        {
            this->setCursor(QCursor(Qt::ClosedHandCursor));
        }
        else if(event->button() == Qt::RightButton)
        {
            if(isSelected())
            {
                this->pen.setColor(Qt::black);
                this->pen.setStyle(Qt::SolidLine);
                this->pen.setWidth(1);
                this->setSelected(false);
            }
            else
            {
                this->pen.setColor(Qt::red);
                this->pen.setWidth(3);
                this->pen.setStyle(Qt::SolidLine);
                this->setFlag(ItemIsSelectable, true);
                this->setSelected(true);
            }
        }
    }
    void shapes::machinators()
    {
        this->pen.setColor(Qt::black);
        this->pen.setStyle(Qt::SolidLine);
        this->pen.setWidth(1);
        this->setSelected(false);
    }
    void shapes::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    {
        this->setCursor(QCursor(Qt::ArrowCursor));
        Q_UNUSED(event);
    }
